<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <!-- <div class="uni-notice" v-if="inputForm.id && inputForm.id.indexOf(',')">{{notice}}</div> -->
        <el-row :gutter="15">
          <el-col :span="22">
            <el-form-item label="发送周期" prop="sendPeriod" :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'发送周期不能为空', trigger:'blur'}
                 ]">
              <el-input-number v-model="inputForm.sendPeriod" placeholder="定位最短发送周期" size="large"
                controls-position="right" :min="3" :max="120"></el-input-number>
              <span class="uni-unit">秒</span>
            </el-form-item>
          </el-col>

          <el-col :span="22">
            <el-form-item label="上报数量" prop="reportNum" :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'上报数量不能为空', trigger:'blur'}
                 ]">
              <el-input-number v-model="inputForm.reportNum" placeholder="最多上报 beacon 数量" size="large"
                controls-position="right" :min="1" :max="20"></el-input-number>
              <span class="uni-unit">个</span>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="搜索超时" prop="overTime" :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'搜索超时不能为空', trigger:'blur'}
                 ]">
              <el-input-number v-model="inputForm.overTime" placeholder="beacon 搜索超时时间" size="large"
                controls-position="right" :min="1" :max="10"></el-input-number>
              <span class="uni-unit">秒</span>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="rssi阀值" prop="rssiThreshold" :rules="[
                  {validator: validator.isIntLteZero, trigger:'blur'},
                  {required: true, message:'rssi阀值不能为空', trigger:'blur'}
                 ]">
              <el-input-number v-model="inputForm.rssiThreshold" placeholder="rssi 过滤阀值" size="large"
                controls-position="right" :min="-100" :max="0"></el-input-number>
              <!-- <span class="uni-unit">秒</span> -->
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="版本号" prop="versionNew" :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'配置版本参数版本号不能为空', trigger:'blur'}
                 ]">
              <!-- <span class="uni-unit">V</span> -->
              <el-input-number v-model="inputForm.versionNew" placeholder="参数版本0~255" size="large"
                controls-position="right" :min="0" :max="255"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="搜索策略" prop="searchType" :rules="[
              {required: true, message:'搜索策略不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.searchType" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lora_search_type')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="运动辅助" prop="moveSwitch" :rules="[
              {required: true, message:'运动辅助不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.moveSwitch" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lora_move_switch')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="rx" prop="rx" :rules="[
              {required: true, message:'rx不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.rx" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lora_move_switch')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="22">
            <el-form-item label="uuid" prop="uuid" :rules="[
              {required: true, message:'uuid不能为空', trigger:'blur'}
              ,{validator: validator.isUUID, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.uuid" placeholder="请填写uuid"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        // notice: '',
        inputForm: {
          id: '',
          rfid: '',
          devEui: '',
          cardType: '',
          appKey: '',
          clazz: '',
          fcntBits: '',
          lorawanVersion: '',
          appEui: '',
          sendPeriod: '',
          moveSwitch: '',
          reportNum: '',
          overTime: '',
          rssiThreshold: '',
          searchType: '',
          versionNew: '',
          rx: '1',
          uuid: 'FDA50693A4E24FB1AFCFC6EB07647825'
        }
      }
    },
    components: {},
    methods: {
      init(method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建工牌参数`
        } else if (method === 'edit') {
          this.title = '修改工牌参数'
        } else if (method === 'view') {
          this.title = '查看工牌参数'
        }

        this.visible = true
        this.loading = false

        if (id.indexOf(",") === -1) {
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            if (method === 'edit' || method === 'view') { // 修改或者查看
              this.loading = true
              this.$http({
                url: `/lora/rfid/jfLoraRfid/queryById?id=${this.inputForm.id}`,
                method: 'get'
              }).then(({
                data
              }) => {
                this.inputForm = this.recover(this.inputForm, data.jfLoraRfid)
                this.inputForm.moveSwitch = this.inputForm.moveSwitch + ''
                this.inputForm.searchType = this.inputForm.searchType + ''
                this.loading = false
              })
            }
          })
        }
      },
      // 表单提交
      doSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: this.inputForm.id && this.inputForm.id.indexOf(",") != -1 ? `/lora/rfid/jfLoraRfid/config` :
                `/lora/rfid/jfLoraRfid/save`,
              method: 'post',
              data: this.inputForm
            }).then(({
              data
            }) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
<style scoped>
  .uni-unit {
    font-size: 14px;
    color: #606266;
    margin: 0 10px;
  }
</style>
